package game.scene;

import game.scene.sceneobject.SceneObject_A;

import java.util.Collections;
import java.util.LinkedList;

import common.Coordinate;
import common.Rectangle;

/**
 * SceneObjectList Provides sorting and conditional transfer capability for a a
 * list of SceneObject_A
 **/

public final class Layer {
	private LinkedList<SceneObject_A> _SceneObjects;
	public Coordinate Translation;
	public boolean Visible;
	public boolean Presort;
	public Layer(Layer_E id) {
		Id = id;
		Visible = true;
		Translation = new Coordinate(0, 0);
		_SceneObjects = new LinkedList<>();
		Presort = true;
	}
	
	public final Layer_E Id;
	/**
	 * Inserts a SceneObject_A to this list.
	 * @param sceneObject - the SceneObject_A to insert.
	 **/
	public boolean insert(SceneObject_A sceneObject) {
		if (sceneObject == null) {
			return false;
		}
		return _SceneObjects.add(sceneObject);
	}
	
	/**
	 * Removes a SceneObject_A from this list.
	 * @param sceneObject - the SceneObject_A to remove
	 * @returns true if the object was in this list, otherwise false.
	 **/
	public boolean remove(SceneObject_A sceneObject) {
		if (sceneObject == null)
			return false;
		return _SceneObjects.remove(sceneObject);
	}
	
	/**
	 * Compare the list with a given rectangle and return any that are overlapping.
	 * @returns a list of overlapping scene objects.
	 **/
	public LinkedList<SceneObject_A> getOverlapping(Rectangle queryRect) {
		LinkedList<SceneObject_A> output = new LinkedList<SceneObject_A>();
		for(SceneObject_A sceneObject : _SceneObjects) {
			if(sceneObject.isOverlapping(queryRect) == true) {
				output.add(sceneObject);
			}
		}
		if(Presort == true) {
			Collections.sort(output);
		}
		return output;
	}

	/**
	 * Perform sorting of this list. See: SceneObject_A and VisibilityDescriptor
	 * for sorting rules.
	 **/
	public void sort() {
		Collections.sort(_SceneObjects);
	}
	/**
	 * @returns the underlying linked list.
	 **/
	public LinkedList<SceneObject_A> getSceneObjects() {
		return _SceneObjects;
	}
	public LinkedList<SceneObject_A> getAll() {
		return _SceneObjects;
	}
	/**
	 * Clears this list.
	 **/
	public void clear() {
		_SceneObjects.clear();
	}
}